package com.bootdoolvillage.wow.api.auction.collect;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import com.bootdoolvillage.wow.api.models.Auction;
import com.bootdoolvillage.wow.api.models.AuctionStamp;
import com.bootdoolvillage.wow.api.util.URLFactory;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;

public class DataCollector {
	private URLFactory urlFactory;
	private Client client;

	public DataCollector(URLFactory urlFactory) {
		this.urlFactory = urlFactory;
		this.client = Client.create();
	}

	private AuctionStamp auctionsURL(String realmName) {
		AuctionStamp result = null;
		WebResource resource = client.resource(urlFactory
				.getAuctionURL(realmName));
		ClientResponse response = resource.accept("application/json").get(
				ClientResponse.class);
		if (response.getStatus() == 200) {
			String jsonString = response.getEntity(String.class);
			JSONObject jsonObject = new JSONObject(jsonString).getJSONArray(
					"files").getJSONObject(0);
			result = new AuctionStamp();
			result.setLastModified(jsonObject.getLong("lastModified"));
			result.setUrl(jsonObject.getString("url"));
		}
		return result;
	}

	public JSONArray auctions(String realmName) {
		JSONArray result = null;
		AuctionStamp auctionStamp = auctionsURL(realmName);
		WebResource resource = client.resource(auctionStamp.getUrl());
		ClientResponse response = resource.accept("application/json").get(
				ClientResponse.class);
		if (response.getStatus() == 200) {
			String jsonString = response.getEntity(String.class);
			result = new JSONObject(jsonString).getJSONObject("auctions")
					.getJSONArray("auctions");
		}
		return result;
	}

	public List<Auction> auctions(String realmName, JSONArray auctions) {
		List<Auction> list = null;
		JSONArray auctionArray = auctions(realmName);
		for (int i = 0; i < auctionArray.length(); i++) {

		}
		return list;
	}
}
